package com.example.demo01.controller;

import com.example.demo01.pojo.FormInfo;
import com.example.demo01.pojo.Record;
import com.example.demo01.service.FormInfoService;
import com.example.demo01.service.RecordService;
import com.example.demo01.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.Calendar;


@Controller
public class HomeController {
    @Autowired
    private UserService userService;
    @Autowired
    private FormInfoService formInfoService;
    @Autowired
    private RecordService recordService;



    @GetMapping("/admin")
    @ResponseBody
    public String loginPage(){
        return "admin";
    }

    @RequestMapping("/")
    public String jumpToEntrance(){
        return "index";
    }

    @RequestMapping("/admin/homePage")
    public String jumpToAdminHomePage(){
        return "admin";
    }

    @RequestMapping("/user/homePage")
    public String jumpToUserHomePage(Model model){
        //准备user信息，根据今天是否提交过，跳转到不同页面
        String username= SecurityContextHolder.getContext().getAuthentication().getName();



        //获取xxxx-xx-xx格式的日期 如2021-06-21
        Calendar cal = Calendar.getInstance();
        int month = cal.get(Calendar.MONTH) + 1;
        int year = cal.get(Calendar.YEAR);
        int day = cal.get(Calendar.DATE);

        String m = "";
        if (month < 10) {
            m = "0" + month;
        } else {
            m = month + "";
        }
        String d = "";
        if (day < 10) {
            d = "0" + day;
        } else {
            d = day + "";
        }
        String date=year+"-"+m+"-"+d;
        System.out.println(date);
        //构造日期接受 xxxx-xx-xx格式



        FormInfo formInfo=formInfoService.getFormInfoByName(username);

        boolean isPostToday=formInfo.getIsPostToday().equals("是");



        if(isPostToday){
            Record record=recordService.getRecordByUsernameAndTime(username,date);
            model.addAttribute("record", record);
            return "user";
        }else{

            model.addAttribute("formInfo",formInfo);
            model.addAttribute("date",date);
            return "userGoToPost";
        }

    }






    //有了spring security后以下登录验证在拦截器部分就完成了，所以不用再在controller再验证密码什么的了
//    @PostMapping("/login")
//    public String login(String username, String password, Model model){
//        //检查账号是否冻结
//        if(!userService.checkActive(username)){
//            model.addAttribute("message", "该账号被冻结！");
//            return "login";
//        }
//        //检验密码
//        if(!userService.isValidUser(username, password)){
//            model.addAttribute("message","账号或密码错误！");
//            return "login";
//        }
//
//
//
//
//
//
//        //获取xxxx-xx-xx格式的日期 如2021-06-21
//        Calendar cal = Calendar.getInstance();
//        int month = cal.get(Calendar.MONTH) + 1;
//        int year = cal.get(Calendar.YEAR);
//        int day = cal.get(Calendar.DATE);
//
//        String m = "";
//        if (month < 10) {
//            m = "0" + month;
//        } else {
//            m = month + "";
//        }
//        String d = "";
//        if (day < 10) {
//            d = "0" + day;
//        } else {
//            d = day + "";
//        }
//        String date=year+"-"+m+"-"+d;
//        System.out.println(date);
//        //构造日期接受 xxxx-xx-xx格式
//
//        String role=userService.getRoleByName(username);
//        if(role.equals("user")){
//
//            FormInfo formInfo=formInfoService.getFormInfoByName(username);
//
//            boolean isPostToday=formInfo.getIsPostToday().equals("是");
//
//            if(isPostToday){
//                Record record=recordService.getRecordByUsernameAndTime(username,date);
//                model.addAttribute("record", record);
//                return "user";
//            }else{
//
//                model.addAttribute("formInfo",formInfo);
//                model.addAttribute("date",date);
//                return "userGoToPost";
//            }
//
//        }
//
//        else{//角色是管理员和超级管理员
//
//            model.addAttribute("role",role);
//            return "admin";
//        }
//    }



}
